package cn.xinyangtong.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/****
 * Http连接工具类
 * @author fengbo
 */
public class HttpUtil {

	/***
	 * 使用get方法连接
	 * @param path
	 * @return
	 */
	public static String doGet(String path) {
		String result = null;
		HttpURLConnection conn = null;
		InputStream in = null;
		try {
			URL url = new URL(path);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5000);
			int code = conn.getResponseCode();
			if (code == 200) {
				in = conn.getInputStream();
				StringBuilder sb = new StringBuilder();
				byte[] buf = new byte[1024];
				int len = -1;
				while ((len = in.read(buf)) != -1) {
					sb.append(new String(buf, 0, len));
				}
				result = sb.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/***
	 * 通过post请求
	 * @param path
	 * @param data
	 * @return
	 */
	public static String doPostJson(String path, String data) {
		// String data = "username="+username+"&password="+password+"";
		String result = null;
		HttpURLConnection conn = null;
		InputStream in = null;
		try {
			URL url = new URL(path);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setConnectTimeout(5000);
			// 设置请求头信息
			conn.setRequestProperty("Content-Type",	"application/json");
			conn.setRequestProperty("Content-Length", data.length() + "");
			// 将拼好的数据以流的形式提交
			conn.setDoOutput(true);// 设置一个标记，允许输出
			conn.getOutputStream().write(data.getBytes());
			int code = conn.getResponseCode();
			if (code == 200) {
				in= conn.getInputStream();
				StringBuilder sb = new StringBuilder();
				int len = -1;
				byte[] buf = new byte[1024];
				while ((len = in.read(buf)) != -1) {
					sb.append(new String(buf, 0, len));
				}
				result = sb.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
//	
//	public static String doPost(String path, String data) {
//		// String data = "username="+username+"&password="+password+"";
//		String result = null;
//		HttpURLConnection conn = null;
//		InputStream in = null;
//		try {
//			URL url = new URL("http://localhost:8081/rest/content/list/89");
//			conn = (HttpURLConnection) url.openConnection();
//			conn.setRequestMethod("POST");
//			conn.setConnectTimeout(5000);
//			// 设置请求头信息
//			conn.setRequestProperty("Content-Type",
//					"application/x-www-form-urlencoded");
//			conn.setRequestProperty("Content-Length", data.length() + "");
//			// 将拼好的数据以流的形式提交
//			conn.setDoOutput(true);// 设置一个标记，允许输出
//			conn.getOutputStream().write(data.getBytes());
//			int code = conn.getResponseCode();
//			if (code == 200) {
//				in= conn.getInputStream();
//				StringBuilder sb = new StringBuilder();
//				int len = -1;
//				byte[] buf = new byte[1024];
//				while ((len = in.read(buf)) != -1) {
//					sb.append(new String(buf, 0, len));
//				}
//				result = sb.toString();
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			if (conn != null) {
//				conn.disconnect();
//			}
//			if (in != null) {
//				try {
//					in.close();
//					in = null;
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
//		return result;
//	}

}
